home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0429.dms / q0429.adf / libray / libsurf / mist.c < prev    next >
C/C++ Source or Header  |  1991-08-08  |  2KB  |  73 lines

  1. /*
  2.  * mist.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: mist.c,v 4.0 91/07/17 14:40:42 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    mist.c,v $
  19.  * Revision 4.0  91/07/17  14:40:42  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "atmosphere.h"
  24. #include "mist.h"
  25.  
  26. Mist *
  27. MistCreate(color, trans, zero, scale)
  28. Color *color, *trans;
  29. Float zero, scale;
  30. {
  31.     Mist *mist;
  32.  
  33.     mist = (Mist *)Malloc(sizeof(Mist));
  34.     mist->color = *color;
  35.     mist->trans = *trans;
  36.     mist->zero = zero;
  37.     mist->scale = 1. / scale;
  38.     return mist;
  39. }
  40.  
  41. /*
  42.  * Add low-altitude mist to the given color.
  43.  */
  44. void
  45. MistApply(mist, ray, pos, dist, color)
  46. Mist *mist;
  47. Ray *ray;
  48. Vector *pos;
  49. Float dist;
  50. Color *color;
  51. {
  52.     Float deltaZ, d, atten;
  53.     extern Float ExpAtten();
  54.  
  55.     deltaZ = mist->scale * (pos->z - ray->pos.z);
  56.     if (fabs(deltaZ) > EPSILON)
  57.         d = (exp(-ray->pos.z*mist->scale + mist->zero) -
  58.                 exp(-pos->z*mist->scale + mist->zero)) / deltaZ;
  59.     else
  60.         d = exp(-pos->z*mist->scale + mist->zero);
  61.  
  62.     dist *= d;
  63.  
  64.     atten = ExpAtten(dist, mist->trans.r);
  65.     color->r = atten*color->r + (1. - atten)*mist->color.r;
  66.  
  67.     atten = ExpAtten(dist, mist->trans.g);
  68.     color->g = atten*color->g + (1. - atten)*mist->color.g;
  69.  
  70.     atten = ExpAtten(dist, mist->trans.b);
  71.     color->b = atten*color->b + (1. - atten)*mist->color.b;
  72. }
  73.